//
//  TabViewController.m
//  pet
//
//  Created by wangdu on 13-1-9.
//  Copyright (c) 2013年 wangdu. All rights reserved.
//

#import "TabViewController.h"
#import "NavigationController.h"
#import "HomeViewController.h"
#import "ActivityViewController.h"
#import "PhotoViewController.h"
#import "Coordinate.h"
#import "Base.h"
@interface TabViewController ()

@end

@implementation TabViewController
-(void)dealloc{

    safeRelease(_tabBarView);
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.tabBar.hidden = YES;
        
        _tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, pMainFrame.size.height-pTabBar_Height, pMainFrame.size.width,pTabBar_Height)];
        int width = pMainFrame.size.width/pTab_Count;
        int height = pTabBar_Height;
        for (int index = 0  ;  index < pTab_Count ; index++){
        
            UIButton *tabButton = [UIButton buttonWithType:UIButtonTypeCustom];
            tabButton.frame = CGRectMake(index * width, 0, width, height);
            [tabButton setTitle:[NSString stringWithFormat:@"%d",index] forState:UIControlStateNormal];
            [tabButton setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
            [tabButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [tabButton addTarget:self action:@selector(tabButtonPressed:) forControlEvents:UIControlEventTouchDown];
            tabButton.tag = index + pTabBar_Tag;
            [_tabBarView addSubview:tabButton];
            
            if (index == 0) {
                tabButton.selected = YES;
            }
            
        }
        
        [self.view addSubview:_tabBarView];
        
        NavigationController *homeNavi  = [self initWithHomeViewController];
        NavigationController *photoNavi = [self initWithPhotoViewController];
        NavigationController *actNavi = [self initWithActivityViewController];
        
        self.viewControllers = [NSArray arrayWithObjects:homeNavi,photoNavi,actNavi,nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NavigationController *)initWithHomeViewController{
    HomeViewController *home     =   [[[HomeViewController alloc] init] autorelease];
    NavigationController *navi   =   [[NavigationController alloc] initWithRootViewController:home];
    return [navi autorelease];
}
-(NavigationController *)initWithPhotoViewController{

    PhotoViewController *photo   =   [[[PhotoViewController alloc] init] autorelease];
    NavigationController *navi   =   [[NavigationController alloc] initWithRootViewController:photo];
    return [navi autorelease];
}
-(NavigationController *)initWithActivityViewController{

    ActivityViewController *act  =   [[[ActivityViewController alloc] init] autorelease];
    NavigationController *navi   =   [[NavigationController alloc] initWithRootViewController:act];
    return [navi autorelease];
}

-(void)tabButtonPressed:(UIButton *)tabButton{
    for(id e in _tabBarView.subviews){
        if ([e isKindOfClass:[UIButton class]]) {
            UIButton *ee = (UIButton *)e;
            ee.selected = NO;
        }
    }
    tabButton.selected = YES;
    self.selectedIndex = tabButton.tag - pTabBar_Tag;
    
}


@end
